﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_YiJianFanKuiBiaoService : It_YiJianFanKuiBiaoService
    {
        public readonly MyContext _Context;
        public t_YiJianFanKuiBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }
        public t_YiJianFanKuiBiao Add(t_YiJianFanKuiBiao t_YiJianFanKuiBiao)
        {
            _Context.t_YiJianFanKuiBiao.Add(t_YiJianFanKuiBiao);
            var test = _Context.SaveChanges();
            return _Context.t_YiJianFanKuiBiao.FirstOrDefault(x => x.BeiZhu1 == t_YiJianFanKuiBiao.BeiZhu1 && x.BeiZhu2 == t_YiJianFanKuiBiao.BeiZhu2 && x.BeiZhu3 == t_YiJianFanKuiBiao.BeiZhu3 && x.BeiZhu4 == t_YiJianFanKuiBiao.BeiZhu4 && x.BeiZhu5 == t_YiJianFanKuiBiao.BeiZhu5 && x.BeiZhu6 == t_YiJianFanKuiBiao.BeiZhu6 && x.BeiZhu7 == t_YiJianFanKuiBiao.BeiZhu7 && x.BeiZhu8 == t_YiJianFanKuiBiao.BeiZhu8 && x.BeiZhu9 == t_YiJianFanKuiBiao.BeiZhu9 && x.BeiZhu10 == t_YiJianFanKuiBiao.BeiZhu10 && x.RenYuanId == t_YiJianFanKuiBiao.RenYuanId && x.FanKuiNeiRong == t_YiJianFanKuiBiao.FanKuiNeiRong && x.YingDaNeiRong == t_YiJianFanKuiBiao.YingDaNeiRong && x.ZhuangTai == t_YiJianFanKuiBiao.ZhuangTai && x.ShiJian == t_YiJianFanKuiBiao.ShiJian);
        }

        public bool Delete(int id)
        {
            _Context.t_YiJianFanKuiBiao.Remove(_Context.t_YiJianFanKuiBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public t_YiJianFanKuiBiao GetById(int id)
        {
            return _Context.t_YiJianFanKuiBiao.Include(x => x.t_RenYuanBiao).FirstOrDefault(x => x.Id == id);
        }

        public List<t_YiJianFanKuiBiao> GetList(int renyuanid)
        {
            return _Context.t_YiJianFanKuiBiao.Include(x => x.t_RenYuanBiao).Where(x => x.RenYuanId == renyuanid).OrderBy(x => x.ZhuangTai).OrderBy(x => Convert.ToDateTime(x.ShiJian)).ToList();
        }

        public string GetListWithFenYe(t_YiJianFanKuiBiao t_YiJianFanKuiBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_YiJianFanKuiBiao);
            var list = new List<t_YiJianFanKuiBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_YiJianFanKuiBiao.Include(x=>x.t_RenYuanBiao).Where(returnwhere).ToList();
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.RenYuanId).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.RenYuanId).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.FanKuiNeiRong).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.FanKuiNeiRong).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.YingDaNeiRong).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.YingDaNeiRong).ToList();
                    }
                    break;
                case 3:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ZhuangTai).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ZhuangTai).ToList();
                    }
                    break;
                case 4:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => Convert.ToDateTime(x.ShiJian)).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => Convert.ToDateTime(x.ShiJian)).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }


        public string ReturnWhere(t_YiJianFanKuiBiao t_YiJianFanKuiBiao)
        {
            string returnstr = " 1=1 ";
            if (t_YiJianFanKuiBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu1 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu2 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu3 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu4 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu5 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu6 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu7 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu8 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu9 + "\")";
            }
            if (t_YiJianFanKuiBiao.BeiZhu10 != null)
            { 
                returnstr = returnstr + " and BeiZhu10.Contains(\"" + t_YiJianFanKuiBiao.BeiZhu10 + "\")";
            }
            if (t_YiJianFanKuiBiao.Id  != 0)
            {
                returnstr = returnstr + " and Id =" + t_YiJianFanKuiBiao.Id + "";
            }
            if (t_YiJianFanKuiBiao.RenYuanId != 0)
            {
                returnstr = returnstr + " and RenYuanId =" + t_YiJianFanKuiBiao.RenYuanId + "";
            }
            if (t_YiJianFanKuiBiao.FanKuiNeiRong != null)
            {
                returnstr = returnstr + " and FanKuiNeiRong.Contains(\"" + t_YiJianFanKuiBiao.FanKuiNeiRong + "\")";
            }
            if (t_YiJianFanKuiBiao.YingDaNeiRong != null)
            {
                returnstr = returnstr + " and YingDaNeiRong.Contains(\"" + t_YiJianFanKuiBiao.YingDaNeiRong + "\")";
            }
            if (t_YiJianFanKuiBiao.ZhuangTai != null)
            {
                returnstr = returnstr + " and ZhuangTai.Contains(\"" + t_YiJianFanKuiBiao.ZhuangTai + "\")";
            }
            if (t_YiJianFanKuiBiao.ShiJian != null)
            {
                returnstr = returnstr + " and ShiJian.Contains(\"" + t_YiJianFanKuiBiao.ShiJian + "\")";
            }
            return returnstr;
        }
    }
}
